#include "precompiled.h"
#include "SceneStack.h"
#include "World_Manager.h"
#include "Core.h"

SceneStack::SceneStack(void)
{
	fsq = 0;
}


SceneStack::~SceneStack(void)
{
}


// Push a Scene onto the stack. The new scene may need a few engine state
// initializations/changes to take charge.
void SceneStack::Push_On_Scene(Scene* pushed_on){
	stack.push( pushed_on );
}

// Pushes the last scene removed with Pop_Off_Scene_Save().
// If there are no scenes to pop (only the root left) then returns false.
bool SceneStack::Push_On_Scene_LastSaved(){
	Scene *tmp = stack_saved.top();
	stack_saved.pop();

	// There wasn't anything. Return false.
	if( tmp == 0 )
		return false;

	stack.push( tmp );
	return true;
}

// Pops the current top of the Scene Stack off. Can return to the scene.
// If there are no scenes to pop (only the root left) then returns false.
bool SceneStack::Pop_Off_Scene_Save(Scene popped_off){
	Scene *tmp = stack.top();
	stack.pop();

	// There wasn't anything. Return false.
	if( tmp == 0 )
		return false;

	stack_saved.push( tmp );
}

// Pops the current top of the Scene Stack off. Cannot return to the scene.
// If there are no scenes to pop (only the root left) then returns false.
bool SceneStack::Pop_Off_Scene_Destroy(Scene popped_off){

	if( stack.top() == 0 )
		return false;

	stack.pop();
	return true;
}


void SceneStack::Update_CurrentScene( GLdouble time ){
	stack.top()->Update( time );
}
void SceneStack::Submit_CurrentScene( glm::mat4 &projview ){
	stack.top()->SubmitToFrame( projview );
}

glm::mat4* SceneStack::GetCurrentCamera(){
	return stack.top()->worldMan->GetCurrentCamera();
}

void SceneStack::Destroy(){
	while( !stack.empty() )
		stack.pop();

	while( !stack_saved.empty() )
		stack_saved.pop();
}

void SceneStack::Load_FSQ(){
	fsq = gs_geompool->get_modelValue( "plane" );
}

void SceneStack::Draw_FSQ(){

	glBindVertexArray(fsq->vao()[0]);
	int num = fsq->numIndices();
	glDrawRangeElements(GL_TRIANGLES,0, num,num , GL_UNSIGNED_SHORT, NULL);
	glBindVertexArray(0);

}

World_Manager* SceneStack::Get_CurrentWorld(){
	return stack.top()->worldMan;
}